﻿using ASmile.CodeFactory.DBModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace ASmile.CodeFactory.DaBLL.Builder
{
    class BuilderOracle : IBuilder
    {
        public BuilderOracle(TConfig config)
            : base(config)
        { }

        DataTable GetTableViewSchema(bool isView)
        {
            var conn = DaBLL.DbHelper.DBConnectionObj;
            var connStr = conn.ConnectionString;
            string findStr = "User ID=";
            int s = connStr.IndexOf(findStr, StringComparison.CurrentCultureIgnoreCase);
            int c = connStr.IndexOf(";", s, StringComparison.CurrentCultureIgnoreCase);
            string uid = connStr.Substring(s + findStr.Length, c - s - findStr.Length);
            DataTable dt;
            if (isView)
            {
                dt = GetSchema("Table_Name", "Views");
            }
            else
            {
                dt = GetSchema("Table_Name", "Tables");
            }
            
            //TODO: 以上可以运行看下UID是否直接从Conn获取，RowFilter 改为 GetSchema 参数过滤
            dt.DefaultView.RowFilter = string.Format("OWNER='{0}'", uid);
            dt.DefaultView.Sort = "Table_Name";
            return dt.DefaultView.ToTable();
        }

        protected override DataTable GetTableSchema()
        {
            return GetTableViewSchema(false);
        }

        protected override DataTable GetViewSchema()
        {
            return GetTableViewSchema(true);
        }
    }
}
